﻿using HMS.DataContract.Request;
using HMS.DataContract.Response;
using HMS.Web.Helpers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.ServiceModel;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

namespace HMS.Web.Controllers
{
    public class PatientController : Controller
    {
        //
        // GET: /Patient/

        public ActionResult Index(int numberOfRecord = 500)
        {
            UserInfo userInfo = (UserInfo)Session["userInfo"];
            var client = InitializeSchedulerClient();
            List<DoctorAppointmentResponse> docApp = new List<DoctorAppointmentResponse>();
            if (userInfo.Detail == "D" && Session["PatientId"] != null)
            {
                docApp = client.GetAppointmentHistoryByPatientId((int)Session["PatientId"], numberOfRecord).ToList<DoctorAppointmentResponse>(); 
            }
            else if(userInfo.Detail == "P")
            {
                docApp =  client.GetAppointmentHistoryByPatientId(userInfo.Id, numberOfRecord).ToList<DoctorAppointmentResponse>();
                Session["PatientId"] = docApp != null && docApp.Count > 0 ? docApp[0].Patient.Id : 0;
            }
            client.Close();
            return View(docApp);

        }

        [Authorize]
        [HttpPost]
        public ActionResult SetPatientSession(int PatientId)
        {
            Session["PatientId"] = PatientId;
            return Json(0);
        }

        [HttpPost]
        public ActionResult GetPatientName(string PatientName)
        {
            var client = InitializeSchedulerClient();
            UserInfo clinic = new UserInfo();
            if (Session["userInfo"] != null)
            {
                clinic = (UserInfo)Session["userInfo"];
            }
            var _lstPatient = client.GetPatientList(PatientName,clinic.ClinicId);
            
            List<PatientViewModel> lstPatient = new List<PatientViewModel>();
            foreach (var item in _lstPatient)
            {
                PatientViewModel patient = new PatientViewModel();
                patient.Id = item.Id;
                patient.Name = item.Detail;
                patient.Email = item.Email;
                patient.Phone = item.Phone;
                lstPatient.Add(patient);
            }
            client.Close();
            return Json(lstPatient);
        }
        [HttpPost]
        public ActionResult GetPatientById(int PatientId,string date)
        {
            DateTime _date = Convert.ToDateTime(date);
            var client = InitializeSchedulerClient();
            var appointment = client.GetPatientById(PatientId,_date);
            DoctorAppointmentResponse doc = new DoctorAppointmentResponse();
            doc.Patient = new PatientResponse();
            doc.Patient.Allergies = appointment.Patient.Allergies;
            doc.AppointmentDateTime = appointment.AppointmentDateTime;
            doc.AppointmentStatus = appointment.AppointmentStatus;
            doc._AppointmentDateTime = appointment.AppointmentDateTime.ToString("dd/MM/yyyy (hh:mm:tt)");
            doc.Purpose = appointment.Purpose;
            doc.Patient.DOB = appointment.Patient.DOB;
            doc.DocLocId = appointment.DocLocId;
            doc.FollowUpStatus = appointment.FollowUpStatus;
            doc.Patient.Gender = new LookUp() { Id = appointment.Patient.Gender.Id };
            doc.Patient.Email = appointment.Patient.Email;
            doc.Patient.Id = appointment.Patient.Id;
            doc.Patient.Name = appointment.Patient.Name;
            doc.Patient.Phone = appointment.Patient.Phone;
            doc.Id = appointment.Id;
            Session["DoctorApptId"] = doc.Id;

            Session["PatientId"] = appointment.Patient.Id;
            Session["DoctorLocationId"] = appointment.DocLocId;
            //Session["PatientId"] = appointment.Patient.Id;
            client.Close();
            return Json(doc);
        }

        [HttpPost]
        public ActionResult SavePatientDetails(PatientResponse patient)
        {
            var client = InitializeSchedulerClient();
            int savedData = client.SavePatientDetails(patient);
            client.Close();
            return Json(savedData);
        }

        /// <summary>
        /// Method to Initialize Scheduler Client
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public SchedulerClient InitializeSchedulerClient()
        {

            var binding = new BasicHttpBinding();
            binding.SendTimeout = new TimeSpan(0, 0, 0, 0, 60000000);
            binding.OpenTimeout = new TimeSpan(0, 0, 0, 0, 10000000);
            binding.MaxReceivedMessageSize = 10000000;
            binding.ReaderQuotas.MaxStringContentLength = 10000000;
            binding.ReaderQuotas.MaxDepth = 10000000;
            binding.ReaderQuotas.MaxArrayLength = 10000000;
            var endpointPath = ConfigurationManager.AppSettings["SchedulerEndpointPath"];
            var endpoint = new EndpointAddress(endpointPath);
            var client = new SchedulerClient(binding, endpoint);
            return client;
        }
    }
}
